package com.yhyvr.console.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.lz.mybatis.jdbc.auto.dao.BoGoodsMapper;
import com.lz.mybatis.jdbc.auto.model.BoGoods;
import com.lz.tool.BeanMapTool;
import com.lz.tool.ListUtils;
import com.lz.tool.UUIDTool;
import com.yhyvr.console.annotation.RepeatSubmit;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lz.kit.LogKit;
import com.lz.tool.LzStringUtils;

import com.yhyvr.pojo.resp.console.BootStrapGridResp;
import com.yhyvr.console.model.login.LoginUserModel;
import com.lz.mybatis.jdbc.auto.dao.BoTasteGoodsMapper;
import com.lz.mybatis.jdbc.auto.model.BoTasteGoods;
import com.lz.mybatis.jdbc.auto.model.BoTasteGoodsExample;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.req.console.BootStrapGridReq;
import com.yhyvr.console.controller.ControllerQueryTool;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * title:
 * </p>
 * <p>
 * description:
 * </p>
 *
 * @create 张钦
 * @update
 * @version 1.0
 * @date
 */
@Controller
public class BoTasteGoodsController {
	@Resource
	private BoTasteGoodsMapper modelMapper;
	@Resource
	private BoGoodsMapper boGoodsMapper ;


	@RequestMapping("/console/bo_taste_goods/add")
	@RepeatSubmit(isAdd = true)
	public String add(){
		return "console/boTasteGoods/boTasteGoods_form";
	}

	@RepeatSubmit(isAdd = true)
	@RequestMapping("/console/bo_taste_goods/edit")
	public String edit(ModelMap modelMap,String id){
			modelMap.put("boTasteGoods", modelMapper.selectByPrimaryKey(id));
		return "console/boTasteGoods/boTasteGoods_form";
	}

	@RequestMapping("/console/bo_taste_goods/list")
	public String list(){
		return "console/boTasteGoods/boTasteGoods_table";
	}

	@RequestMapping("/console/bo_taste_goods/json")
	public @ResponseBody BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		BoTasteGoodsExample boTasteGoodsExample = new BoTasteGoodsExample();
        ControllerQueryTool.setBoTasteGoodsCriteria(bootGridReq.getQuery(),boTasteGoodsExample.createCriteria());
		List<BoTasteGoods> boTasteGoodsList = modelMapper.selectByExample(boTasteGoodsExample);
		List<JSONObject> list = BeanMapTool.objectsToJsonObjects(boTasteGoodsList);
		if(ListUtils.isNotBlank(list)){
			for (JSONObject jsonObject : list) {
				BoGoods goods = boGoodsMapper.selectByPrimaryKey(jsonObject.getString("boGoodsId"));
				jsonObject.put("goodsName",goods.getGoodsTitle());
			}
		}
		return new BootStrapGridResp(page.getTotal(),list);
	}

	@RepeatSubmit(isAdd = false)
	@RequestMapping("/console/bo_taste_goods/save")
	public @ResponseBody HttpResult save(String formObjectJson){
		try{
		BoTasteGoods boTasteGoods= JSON.parseObject(formObjectJson,BoTasteGoods.class);
			String id=boTasteGoods.getBoTasteGoodsId();
		if (id!=null) {
			modelMapper.updateByPrimaryKeySelective(boTasteGoods);
			return CommonResp.getSuccess();
		}else{
			boTasteGoods.setBoTasteGoodsId(UUIDTool.getUUID());
			modelMapper.insertSelective(boTasteGoods);
			return CommonResp.getSuccess();
		}
		}catch(Exception e){
			LogKit.error(e.getMessage(),e);
			return CommonResp.getError();
		}

	}

	@RequestMapping("/console/bo_taste_goods/delete")
	public @ResponseBody HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
		modelMapper.deleteByPrimaryKey(String.valueOf(id));
		}
		return CommonResp.getSuccess();
	}
}
